home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / Newton Platform Info / Newton 2.0 Sample Code / Desktop Connectivity / SoupDrink-2 / SoupDrink-Windows-1 / SOUPDRNK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-12  |  7.8 KB  |  266 lines  |  [TEXT/R*ch]

  1. /*----------------------------------------------------------------------------------
  2.    File:        SoupDrnk.C
  3.    Contains:    Sample application for the Desktop Integration Libraries
  4.    Written By:    Rob Langhorne, J. Christopher Bell and David Fedor
  5.    Copyright 1995-1996 by Apple Computer, Inc.  All rights reserved.
  6.  
  7.    This file contains nearly all of the Windows-specific code.  It is not
  8.    intended to be good Windows sample code, but it works to demonstrate
  9.    the DILs.
  10.  
  11.    Nearly all of the DIL code can be found in Engine.c, except for the 
  12.    calls in here which initialize the DILs and allocate the pipe object.
  13.    
  14.   --------------------------------------------------------------------------------*/
  15.                                
  16. #include <windows.h>   
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>        
  20.  
  21. #include "DILCPIPE.H"
  22. #include "HLFDIL.h"
  23. #include "SoupDrnk.h"
  24. #include "Engine.h"
  25.  
  26. /*
  27. **    Function Prototypes
  28. */
  29. long FAR PASCAL  WndProc (HWND, UINT, UINT, LONG) ;
  30. BOOL FAR PASCAL  AboutDlgProc (HWND, UINT, UINT, LONG) ;
  31. BOOL FAR PASCAL  InputDlgProc (HWND, UINT, UINT, LONG);
  32.  
  33. CommErr ReportError(CommErr fErr, char* mes);
  34.  
  35. /*
  36. **    Global variables (some used by Engine.c)
  37. */
  38. char szAppName [] = "SoupDrink" ;
  39. char gMessageText [255];
  40. HANDLE FAR gXInst;
  41. HWND    gWindow;
  42. static HANDLE  hInstance, inputInstance ;
  43. FARPROC gInputProc;
  44. int     gInputMode = 0;
  45. char    gTempBuffer[1000];
  46. char    gReceivedString[1000];
  47. int        gReceivedStringLen;
  48. void     *ourPipe ;
  49. void     *gThisObject;
  50.  
  51. CommErr ReportError(CommErr fErr, char* mess)
  52. {
  53.     char  s[250];
  54.     sprintf(s, "Error: %s ... err %d", mess, fErr);
  55.     MessageBox (0, s, "SoupDrink", MB_ICONEXCLAMATION | MB_OK) ;
  56.     
  57.     return(fErr);                                                          
  58. }
  59.  
  60. void PostAlertMessage(char*a, char*b, char*c, char*d)
  61. {
  62.     char s[2000];
  63.     
  64.     sprintf(s, "%s%s%s%s", a,b,c,d); 
  65.     MessageBox (0, s, "SoupDrink", MB_ICONEXCLAMATION | MB_OK) ;
  66. }
  67.  
  68. void inputDialog(char *soupname, char* defaultSoup, char* instruction) {
  69.     if (DialogBoxParam (inputInstance, "IDD_INPUTDIALOG", NULL, gInputProc, (long) defaultSoup))
  70.         strcpy(soupname, gMessageText);
  71.     else
  72.         *soupname = '\0';
  73. }
  74.  
  75. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  76.                     LPSTR lpszCmdLine, int nCmdShow)
  77.      {
  78.      HWND     hwnd ;
  79.      MSG      msg ;
  80.      WNDCLASS wndclass ;
  81.      CommErr  err;
  82.  
  83.      if (!hPrevInstance) 
  84.           {
  85.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  86.           wndclass.lpfnWndProc   = WndProc ;
  87.           wndclass.cbClsExtra    = 0 ;
  88.           wndclass.cbWndExtra    = 0 ;
  89.           wndclass.hInstance     = hInstance ;
  90.           wndclass.hIcon         = LoadIcon (hInstance, "SoupDrink") ;
  91.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  92.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  93.           wndclass.lpszMenuName  = szAppName ;
  94.           wndclass.lpszClassName = szAppName ;
  95.  
  96.           RegisterClass (&wndclass) ;
  97.           }
  98.  
  99.         strcpy( gMessageText, "");        // clear out gMessageText
  100.      gXInst = hInstance;
  101.  
  102.      hwnd = CreateWindow (szAppName, "SoupDrink",
  103.                           WS_OVERLAPPEDWINDOW,
  104.                           CW_USEDEFAULT, CW_USEDEFAULT,
  105.                           CW_USEDEFAULT, CW_USEDEFAULT,
  106.                           NULL, NULL, hInstance, NULL) ;
  107.  
  108.      ShowWindow (hwnd, nCmdShow) ;
  109.      UpdateWindow (hwnd) ;
  110.  
  111.      gWindow = hwnd;
  112.  
  113.      // Initialize the DILs
  114.      err = CDInitCDIL();
  115.      err = FDInitFDIL();
  116.  
  117.      // Create a pipe
  118.      ourPipe = CDCreateCDILObject ();
  119.      CDSetApplication(ourPipe, (short) gXInst);
  120.  
  121.        while (TRUE)
  122.        {
  123.                if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  124.               {   
  125.                   if (msg.message == WM_QUIT)
  126.                       break;
  127.                   
  128.                   TranslateMessage (&msg) ;
  129.                   DispatchMessage (&msg) ;
  130.               }  
  131.              else
  132.              {            //    Perform idle processing
  133.                 if (ourPipe)
  134.                     CDIdle (ourPipe);
  135.              }
  136.          }
  137.  
  138.      //    Cleanup DIL
  139.     if (ourPipe) {
  140.          err = CDPipeDisconnect(ourPipe);
  141.          CDDisposeCDILObject(ourPipe);
  142.     }
  143.  
  144.     CDDisposeCDIL() ;
  145.  
  146.     return msg.wParam ;
  147.     }
  148.  
  149. CommErr InitializePipe(void)
  150. {
  151.     CommErr err;
  152.  
  153.     // Right now, the port is hard-coded.  It would probably be
  154.     // good to let the user choose between com ports...
  155.     err = CDPipeInit(ourPipe, "MNP", "", "COM1:38400,n,8,1", "COM1", 1024, 1024);    
  156.  
  157.     if (err)
  158.         return (int) ReportError(err, "CDPipeInit error");
  159. }
  160.  
  161.  
  162. long FAR PASCAL  WndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam)
  163. {
  164.      static FARPROC lpfnAboutDlgProc ;
  165.      static FARPROC lpfnInputDlgProc ;
  166.      HMENU           hMenu ;
  167.      Boolean         eom = false;
  168.      CommErr        err;
  169.                       
  170.      switch (message)
  171.           {
  172.            case WM_CREATE:
  173.                hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
  174.  
  175.                lpfnAboutDlgProc = MakeProcInstance ((FARPROC) AboutDlgProc,
  176.                                                     hInstance) ;
  177.  
  178.                inputInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
  179.  
  180.                lpfnInputDlgProc = MakeProcInstance ((FARPROC) InputDlgProc,
  181.                                                     inputInstance) ;
  182.                gInputProc = lpfnInputDlgProc;
  183.                return 0 ;
  184.  
  185.          case WM_COMMAND:
  186.                hMenu = GetMenu (hwnd) ;
  187.  
  188.                switch (wParam)
  189.                     {
  190.                     case IDM_EXIT:
  191.                          SendMessage (hwnd, WM_CLOSE, 0, 0L) ;
  192.                          return 0 ;
  193.  
  194.                     case IDM_NEWNAME:         // Send a name card to the Newton device
  195.                         if (ourPipe==0)
  196.                             PostAlertMessage("Please make a connection first.","","","");
  197.                         else {
  198.                             err = UploadNewName();
  199.                             if (err)
  200.                                 PostAlertMessage("An error was returned while creating a new name card.",ErrorStrings(err, gTempBuffer),"","");
  201.                         }
  202.                         return 0 ;  
  203.                           
  204.                     case IDM_READ:           // Read a soup from the Newton device
  205.                         if (ourPipe) {   
  206.                             err = SoupDrink () ;
  207.                             if (err)
  208.                                 PostAlertMessage("An error was returned from that Soup Drink operation.", ErrorStrings(err, gTempBuffer), "", "");
  209.                           }
  210.                           break;
  211.  
  212.                     case IDM_ABOUT:
  213.                           DialogBox (hInstance, "AboutBox", hwnd, lpfnAboutDlgProc) ;
  214.                          return 0 ;
  215.                     }
  216.                break ;
  217.  
  218.           case WM_DESTROY :
  219.                PostQuitMessage (0) ;
  220.                return 0 ;
  221.           }
  222.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  223. }
  224.  
  225. BOOL FAR PASCAL AboutDlgProc (HWND hDlg, UINT message, UINT wParam, LONG lParam)
  226. {
  227.      switch (message)
  228.           {
  229.           case WM_INITDIALOG:
  230.                return TRUE ;
  231.  
  232.           case WM_COMMAND:
  233.                switch (wParam)
  234.                     {
  235.                         case IDOK:
  236.                         case IDCANCEL:
  237.                            EndDialog (hDlg, 0) ;     
  238.                            return TRUE ;
  239.                     }
  240.                break ;
  241.           }
  242.      return FALSE ;
  243. }
  244.  
  245. BOOL FAR PASCAL  InputDlgProc (HWND hDlg, UINT message, UINT wParam, LONG lParam)
  246. {
  247.      switch (message)
  248.           {
  249.           case WM_INITDIALOG:
  250.                SetDlgItemText(hDlg, IDC_EDIT1, (LPSTR)lParam);
  251.                return TRUE ;
  252.  
  253.           case WM_COMMAND:
  254.                switch (wParam)
  255.                     {
  256.                     case IDOK:
  257.                     case IDCANCEL:
  258.                          GetDlgItemText(hDlg, IDC_EDIT1, (LPSTR)&gMessageText, 255);
  259.                          EndDialog (hDlg, (wParam==IDOK)) ;
  260.                          return TRUE ;
  261.                     }
  262.                break ;
  263.           }
  264.      return FALSE ;
  265. }
  266.